package com.qeng.aiwanba.net;

import android.util.Base64;

import com.qeng.aiwanba.util.Constants;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class QYRSAUtil {

    private static final String KEY_ALGORITHM = "RSA";

//    private static final String privateKeyString = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALa+aRL5puzyhc7qofqGkz/ayUPZPj0X3gwIBXMcyIehqvMK65ycK3AVtm569c4NxE9rP7dV4nhJufj4O2Snc08MqXKRDadKRLYOWIwkcbPY5MgU66wVZZPl3lsBwPfpR9hCMBGk14IGMnMgKb8r6JkQRDM6Tac5ZvU8cDFhgg9HAgMBAAECgYB3LpvsqjF523n1RwU5FMe4H4Kf43E5+vTwN9MGDkGiY4fQVtDvJNMXmLGQQFVLe6MKgNHfSf4XoU3O2GYz8Rm72myCiCzHoYL7Mq5gzbKyFyG6CjO1oiogvHu4IfN/GfyOkNMl84BBAEkLP+TD6YRDC3ccTjdd4VweXqB0G+sccQJBAPLK0UUHXcPp27tp9GKj67tzNnRZJJBHPJ98C7kEEeHEYBhzeCHhyw85DBaw4YfZokOAMHCy3CA2+iEvPMmagyUCQQDAr1gEq7E8Z+GuVUpinGToPySSfJvrtPIS7qBSsczBz4gicXzUjvsoOBEPJmeQStS70I3aoOn5uyD6p4OSGXL7AkBg9GMrWKy1/p5zsfUYqvMJERIu1iZGw1vPKF8aDCkD6kKhoQLZLOzE8HgGW65EbMFiiT9O0e4bozixpcZ1j5FNAkBBVnQDSXEVKaimY/eQTrYM5vRw095PZBE+CDrHL9Fd2xCvlUoeDcnuzmxCdeRiUm2mrkab3zrX2vOI62q7DPQnAkEArsEmwm1SvZOryN00io7RX9YKhc30+bWumzvaAE6W9CDncsZEnZeyD/S7aQ2z7t/entls1B11IZyFBK6vQB5BbQ==";
//    private static final String publicKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2vmkS+abs8oXO6qH6hpM/2slD2T49F94MCAVzHMiHoarzCuucnCtwFbZuevXODcRPaz+3VeJ4Sbn4+Dtkp3NPDKlykQ2nSkS2DliMJHGz2OTIFOusFWWT5d5bAcD36UfYQjARpNeCBjJzICm/K+iZEEQzOk2nOWb1PHAxYYIPRwIDAQAB";

    public static String sign(String val) {
        try {
            Signature sign = Signature.getInstance("SHA1withRSA");
            sign.initSign(restorePrivateKey(Base64.decode(Constants.privateKey, Base64.DEFAULT)));
            byte[] data = val.getBytes();
            sign.update(data);
            byte[] signature = sign.sign();
            String signString = new String(Base64.encode(signature, Base64.DEFAULT));

            /*
            Signature verifySign = Signature.getInstance("SHA1withRSA");
            verifySign.initVerify(restorePublicKey(Base64.decode(publicKeyString, Base64.DEFAULT)));
            verifySign.update(data);
            boolean flag = verifySign.verify(Base64.decode(signString, Base64.DEFAULT));
            System.out.println(flag);
            */

            return signString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 还原私钥，PKCS8EncodedKeySpec 用于构建私钥的规范
     *
     * @param keyBytes
     * @return
     */
    public static PrivateKey restorePrivateKey(byte[] keyBytes) {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
        try {
            KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
            PrivateKey privateKey = factory.generatePrivate(pkcs8EncodedKeySpec);
            return privateKey;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 还原公钥，X509EncodedKeySpec 用于构建公钥的规范
     *
     * @param keyBytes
     * @return
     */
    public static PublicKey restorePublicKey(byte[] keyBytes) {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
        try {
            KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
            PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec);
            return publicKey;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}